package org.ykh.gmall.product.service.impl;

import org.ykh.gmall.product.mapper.BaseCategoryTrademarkMapper;
import org.ykh.gmall.product.mapper.BaseTraderMarkMapper;
import org.ykh.gmall.product.BaseCategoryTrademark;
import org.ykh.gmall.product.BaseTrademark;
import org.ykh.gmall.product.CategoryTrademarkVo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.ykh.gmall.product.service.BaseCategoryTrademarkService;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@SuppressWarnings("all")
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper,BaseCategoryTrademark> implements
        BaseCategoryTrademarkService {

    @Autowired
    private BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;

    @Autowired
    private BaseTraderMarkMapper baseTraderMarkMapper;


    /**
     * 根据category3Id获取品牌列表
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {

        //根据三级分类id查询关联的关系集合
        LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id,category3Id);

        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(queryWrapper);


        /**
         * stream流中：
         * map(): 对当前集合遍历过程中做处理的话，选中map
         *
         * filter(): 在集合中的多个数据排斥其中几个。
         *
         */
        //遍历集合处理获取品牌id集合
        if(!CollectionUtils.isEmpty(baseCategoryTrademarkList)){

            List<Long> tradermarkIds = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> {
                //只返回品牌id即可
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());

            //根据id集合查询品牌集合数据
            List<BaseTrademark> baseTrademarkList = baseTraderMarkMapper.selectBatchIds(tradermarkIds);
            return baseTrademarkList;
        }

        return new ArrayList<>();
    }

    /**
     * 删除分类品牌关联
     * @param category3Id
     * @param trademarkId
     */
    @Override
    public void remove(Long category3Id, Long trademarkId) {

        //select*from base_category_trademark where category3_id=? and trademark_id=?

        //封装删除条件
        LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id,category3Id);
        queryWrapper.eq(BaseCategoryTrademark::getTrademarkId,trademarkId);
        //执行删除
        baseCategoryTrademarkMapper.delete(queryWrapper);

    }

    /**
     * 根据category3Id获取可选品牌列表
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {

        //根据三级分类id查询关联的关系集合
        LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id,category3Id);

        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(queryWrapper);


        /**
         * stream流中：
         * map(): 对当前集合遍历过程中做处理的话，选中map
         *
         * filter(): 在集合中的多个数据排斥其中几个。
         *
         */
        //遍历集合处理获取品牌id集合
        if(!CollectionUtils.isEmpty(baseCategoryTrademarkList)){

            List<Long> tradermarkIds = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> {
                //只返回品牌id即可
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());


            //查询所有品牌集合
            List<BaseTrademark> baseTrademarkList = baseTraderMarkMapper.selectList(null);
            //处理返回符合条件的品牌对象
            List<BaseTrademark> baseTrademarks = baseTrademarkList.stream().filter(baseTrademark -> {

                //表示是否包含在已经关联的id集合中
                return !tradermarkIds.contains(baseTrademark.getId());
            }).collect(Collectors.toList());


            return baseTrademarks;
        }





        return baseTraderMarkMapper.selectList(null);
    }

    /**
     * 保存分类品牌关联
     * @param categoryTrademarkVo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(CategoryTrademarkVo categoryTrademarkVo) {

        //获取关联的品牌id
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();
        //组合数据
        List<BaseCategoryTrademark> baseCategoryTrademarks = trademarkIdList.stream().map(trademarkId -> {

            //创建分类品牌对象
            BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
            baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
            baseCategoryTrademark.setTrademarkId(trademarkId);


            return baseCategoryTrademark;
        }).collect(Collectors.toList());


        //批量保存
        this.saveBatch(baseCategoryTrademarks);

    }
}
